package com.xxx.car.web.api;

import com.xxx.car.entity.vo.request.ReqFeedBack;
import com.xxx.car.entity.vo.request.ReqFeedBackGet;
import com.xxx.car.entity.vo.request.ReqFeedBackQuery;
import com.xxx.car.entity.vo.response.RespFeedBack;
import com.xxx.car.service.FeedbackService;
import com.xxx.common.base.enums.ApiResponseEnum;
import com.xxx.common.base.vo.APIResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by xuwenjie on 2017/6/22.
 */
@Controller
@RequestMapping("/api/feedback")
public class ApiFeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    @RequestMapping("/list")
    @ResponseBody
    public APIResponse list(@Valid @ModelAttribute ReqFeedBackQuery feedBackQuery, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        List<RespFeedBack> respFeedBackList = feedbackService.query(feedBackQuery);
        return APIResponse.returnSuccess(respFeedBackList);
    }

    @RequestMapping("/get")
    @ResponseBody
    public APIResponse detail(@Valid @ModelAttribute ReqFeedBackGet feedBackGet, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        RespFeedBack respFeedBack = feedbackService.getByIdAndUser(feedBackGet.getId(), feedBackGet.getUserId());
        return APIResponse.returnSuccess(respFeedBack);
    }

    @RequestMapping("/save")
    @ResponseBody
    public APIResponse save(@Valid @ModelAttribute ReqFeedBack reqFeedBack, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        feedbackService.save(reqFeedBack);
        return APIResponse.returnSuccess();
    }

}
